package com.example.hasee.zhcd.Activities.ShangWu;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.example.hasee.zhcd.Adapters.MyFragmentPagerAdapter;
import com.example.hasee.zhcd.R;

import java.util.ArrayList;

/**
 * Created by hasee on 2017/10/23.
 */

public class ReserveFundActivity extends FragmentActivity implements View.OnClickListener {
    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentList;
    private TextView text1, text2;
    private int currIndex;// 当前页卡编号
    private Context context;
    private ImageView back;
    private ImageView home;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        ActivityWidth = wm.getDefaultDisplay().getWidth();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fund_query);
        findView();
        title.setText("备用金查询");
        back.setOnClickListener(this);
        home.setVisibility(View.GONE);
        context = ReserveFundActivity.this;
        InitButtonView();
        initLineView();
        InitViewPager();

    }

    /*
     * 初始化标签名
     */
    public void InitButtonView() {
        text1 = (TextView) findViewById(R.id.text_guid1);
        text2 = (TextView) findViewById(R.id.text_guid2);
        text1.setTextColor(this.getResources().getColor(R.color.themeBlue));
        text2.setTextColor(this.getResources().getColor(R.color.Grey));

        text1.setOnClickListener(new txListener(0));
        text2.setOnClickListener(new txListener(1));
    }

    public class txListener implements View.OnClickListener {
        private int index = 0;

        public txListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(index);
        }
    }

    // 底部滑动线条
    private View buttomView;
    // 当前屏幕宽度
    private int ActivityWidth;

    // 初始化底部滑动线条
    private void initLineView() {
        // 初始化宽高
        buttomView = (View) findViewById(R.id.view_buttom);
        LayoutParams params = new LayoutParams(ActivityWidth / 2, 5);
        buttomView.setLayoutParams(params);
        // 初始化颜色
        buttomView.setBackgroundColor(this.getResources().getColor(
                R.color.themeBlue));
    }

    /*
 * 初始化ViewPager
 */
    public void InitViewPager() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        fragmentList = new ArrayList<Fragment>();
        Fragment oneFragment = RFShipFragment.newInstance(context);
        Fragment twoFragment = RFCompanyFragment.newInstance(context);
        fragmentList.add(oneFragment);
        fragmentList.add(twoFragment);
        // 给ViewPager设置适配器
        viewPager.setAdapter(new MyFragmentPagerAdapter(
                getSupportFragmentManager(), fragmentList));
        viewPager.setCurrentItem(0);// 设置当前显示标签页为第一页
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            public void onPageSelected(int position) {
                slideView(position);
                currIndex = position;
                if (position == 0) {
                    text1.setTextColor(ReserveFundActivity.this.getResources()
                            .getColor(R.color.themeBlue));
                    text2.setTextColor(ReserveFundActivity.this.getResources()
                            .getColor(R.color.Grey));
                } else {
                    text1.setTextColor(ReserveFundActivity.this.getResources()
                            .getColor(R.color.Grey));
                    text2.setTextColor(ReserveFundActivity.this.getResources()
                            .getColor(R.color.themeBlue));
                }
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    // 底部滑动， positionId， 你要滑动到的位置
    private void slideView(int positionIndex) {
        TranslateAnimation tran = null;
        tran = new TranslateAnimation(calcPosition(currIndex),
                calcPosition(positionIndex), 0, 0);
        tran.setDuration(300);
        tran.setFillAfter(true);
        buttomView.startAnimation(tran);
    }

    // 计算位置 position 需要移动到的位置
    private int calcPosition(int positionIndex) {
        int position = ActivityWidth / 2;
        int currentPosition = 0;
        for (int i = 0; i < positionIndex; i++) {
            currentPosition += position;
        }
        return currentPosition;
    }

    public void findView() {
        title = (TextView) findViewById(R.id.centertitle);
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
